<?php
/**
 * Created by PhpStorm.
 * User: hebidu
 * Date: 15/7/2
 * Time: 13:46
 */

namespace Admin\Controller;


use Admin\Api\DatatreeApi;
use Admin\Api\MemberApi;
use Shop\Api\WalletApplyApi;
use Shop\Model\WalletApplyModel;
use Shop\Api\WalletHisApi;
use Admin\Api\MessageApi;
use Admin\Model\MessageModel;

class WithdrawController extends AdminController{
    protected  function _initialize(){
        parent::_initialize();
    }

    /**
     * 提现审核
     */
    public function verify(){


        $uid    = I('post.uid',0);
        $where  = array();
        $params = array();
        if($uid != 0){
            $where['uid']  = $uid;
            $params['uid'] = $uid;
            $r = apiCall(MemberApi::GET_INFO,array(array('uid'=>$uid)));
            if(!$r['status']){
                LogRecord('INFO:' . $r['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
                $this -> error($r['info']);
            }
            if(empty($r['info'])){
                LogRecord('INFO:' . $r['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
                $this -> error('uid错误');
            }
            $nickname = $r['info']['nickname'];
        }else{
            $nickname = '';
        }

        $valid_status    = I('valid_status',-2);    //2驳回 0待处理 1打款
        $this -> assign('valid_status',$valid_status);

        if(!in_array($valid_status,array(-2,0,1,2))) $this->error('参数错误');
        if($valid_status != -2) $where['valid_status'] = $valid_status;

//      $where['valid_status'] = 2;
        $page   = array('curpage'=>I('get.p',0),'size'=>10);
        $order  = " create_time desc ";
        $result = apiCall(WalletApplyApi::QUERY,array($where,$page,$order,$params));
        ifFailedLogRecord($result,__FILE__.__LINE__);
        $list = $this->type2dtree($result['info']['list']);
        $this->assign('uid',$uid);
        $this->assign('nick',$nickname);
        $this->assign("list",$list);
        $this->assign("show",$result['info']['show']);
        $this->display();
    }

    /**
     * 提现历史查询
     */
    public function query(){

        $arr = getDataRange(3);
		//dump($arr);
        $uid = I('uid',0);
        $where = array();
        $params = array();
        $status = I('status',"");
        $nickname = '';
        if($uid != 0){
            $where['uid'] = $uid;
            $params['uid'] = $uid;
            $r = apiCall(MemberApi::GET_INFO,array(array('uid'=>$uid)));
            if(!$r['status']){
                LogRecord('INFO:' . $r['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
                $this -> error($r['info']);
            }
            if(empty($r['info'])){
                LogRecord('INFO:' . $r['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
                $this -> error('uid错误');
            }
            $nickname = $r['info']['nickname'];
        }
        $startdatetime = urldecode($arr[0]);
        $enddatetime   = urldecode($arr[1]);
        $params = array('startdatetime' => $startdatetime, 'enddatetime' => ($enddatetime),'uid'=>$uid);
        $startdatetime = strtotime($startdatetime);
        $enddatetime = strtotime($enddatetime);

        if ($startdatetime === FALSE || $enddatetime === FALSE) {
            LogRecord('INFO:' . $result['info'], '[FILE] ' . __FILE__ . ' [LINE] ' . __LINE__);
            $this -> error(L('ERR_DATE_INVALID'));
        }

        $where['create_time'] = array( array('EGT', $startdatetime), array('elt', $enddatetime), 'and');
        $page = array('curpage'=>I('get.p',0),'size'=>10);
        $order = " create_time desc ";

        $result = apiCall(WalletHisApi::QUERY,array($where,$page,$order,$params));
        //dump($where);
        //dump($result);
        ifFailedLogRecord($result,__FILE__.__LINE__);
        $list = $this->type2dtree($result['info']['list']);
        $list = int_to_string($list,"status",array(2=>"待审","1"=>"通过",3=>"驳回"));

        $this->assign("uid",$uid);
        $this->assign('nickname',$nickname);
        $this->assign("startdatetime",$startdatetime);
        $this->assign("enddatetime",$enddatetime);
        $this->assign("list",$list);
        $this->assign("show",$result['info']['show']);
        $this->display();
    }


    public function viewAccount(){
        $id = I('get.id',0);

        $result = apiCall(MemberApi::GET_INFO, array(array("uid"=>$id)));
        if(!$result['status']){
            $this->error($result['info']);
        }

        $this->assign("userinfo",$result['info']);

        $result = apiCall(WalletApplyApi::GET_INFO, array(array('id'=>$id)));

        if(!$result['status']){
            $this->error($result['info']);
        }

        $this->assign("withdraw",$result['info']);
        $this->display();
    }

    //提现转账
    public function pass(){
        $id = I('get.id',0);
        //记录检查
        $r = apiCall(WalletApplyApi::GET_INFO,array(array('id'=>$id)));
        if(!$r['status']) $this->error($r['info']);
        if(empty($r['info'])) $this->error('记录id错误');
        $status = intval($r['info']['valid_status']);
        $uid = $r['info']['uid'];
        if($status !== 0) $this->error('该记录已处理');
        $r = apiCall(WalletApplyApi::PASS_WALLETAPPLY,array($id,$uid));
        if(!$r['status']) $this->error($r['info']);
        else $this->success($r['info']);
    }

    //提现驳回 - post
    public function deny(){
        $id  = I('post.id',0);
        $msg = I('post.reason','');
        //记录检查
        $r = apiCall(WalletApplyApi::GET_INFO,array(array('id'=>$id)));
        if(!$r['status'])     $this->error($r['info']);
        if(empty($r['info'])) $this->error('记录id错误');
        $status = intval($r['info']['valid_status']);
        if($status !== 0) $this->error('该记录已处理');

        if(!$msg) $this->error('驳回理由不得为空');
        //修改记录状态
        $uid = intval($r['info']['uid']);
        $r = apiCall(WalletApplyApi::SAVE,array(
            array('id'=>$id),
            array('valid_status'=>WalletApplyModel::DENY,'reply_msg'=>$msg,'op_uid'=>UID)
            ));
        ifFailedLogRecord($r,__FILE__.__LINE__);
        if(!$r['status']) $this->error($r['info']);
        //信息推送
        $entity = array(
          'from_id' =>0,
          'title'   =>'提现申请变动',
          'content' =>$msg,
          'summary' =>$msg,
          'extra'   =>'', //消息记录中的
        );
        $after_open = array('type'=>'go_activity','param'=>MessageModel::MESSAGE_SYSTEM_ACTIVITY,'extra'=>array()); //推送参数
        $r = apiCall(MessageApi::PUSH_MESSAGE_WITH_TYPE,array(MessageModel::MESSAGE_SYSTEM,$entity,$uid,false,$after_open));
        if(!$r['status']) $this->success($r['info']);
        $this->success("操作成功!");
    }


    /*private*/
    private  function type2dtree($list){
        $dtree_ids = array(-1);
        foreach($list as $vo){
            array_push($dtree_ids,$vo['dtree_account_type']);
        }

        $where = array(
            'id'=>array("in",$dtree_ids)
        );

        $result = apiCall(DatatreeApi::QUERY_NO_PAGING,array($where));
        ifFailedLogRecord($result,__LINE__.__LINE__);
        $dtree_type = $result['info'];

        foreach($list as &$vo){
            foreach ($dtree_type as $type) {
                if($vo['dtree_account_type'] == $type['id']){
                    $vo['_account_type'] = $type['name'];
                }
            }
        }
        return $list;
    }
}